﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace translator
{
    public class CreateDBFromCurrentCommand : ICommand
    {
        public MainWindow mainWindow;

        public CreateDBFromCurrentCommand(MainWindow mainWindow)
        {
            this.mainWindow = mainWindow;
            
        }

        public event EventHandler CanExecuteChanged {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object parameter)
        {
            //mainWindow.Log("CanExecute" + (mainWindow.filepath == null));
            if (mainWindow.filepath == "") { return false; }
            return true;
        }

        public void Execute(object parameter)
        {
            CreateDBFromCurrent();
        }
        public string filName;
        public void CreateDBFromCurrent()
        {
            //  还没有实现新建String表

            mainWindow.Log("Try to load or new String table...");
            mainWindow.stringEditWindow.ToHideWindow();
            mainWindow.Focusable = false;

            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "Document"; 
            dlg.DefaultExt = ".db"; 
            dlg.Filter = ""; // Filter files by extension
            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                // Save document
                filName = dlg.FileName;
                mainWindow.Log("filename: "+filName);
                new CreateDBWindow(this, mainWindow.OriginalLanguage);


                
                return;
            }
            mainWindow.Log("Cancelled");

            
        }
        public void Cancel()
        {
            if (mainWindow.Focusable == false)
            {
                mainWindow.Focusable = true;
                mainWindow.Log("Load cancelled.");
            }
        }

        public void NewStringTableFromCurrentStringTable(Lang newLang, string fileName)        //从当前String表生成新的String表   (参数1:新String表Translated语言,参数2:新String表存放路径)
        {

            mainWindow.disConnect();

            Const.CopyFile(mainWindow.filepath, fileName);
            if (!mainWindow.OpenDatabase(fileName)) { mainWindow.Log("End Load Process."); return; }
            mainWindow.ReadDataBase();
            mainWindow.GetHintTable();
            mainWindow.StringHeader.Header = "String[" + mainWindow.Translated + "/" + mainWindow.model.StringLst.Count + "]";     //UI刷新,写在这里不妥当
            mainWindow.Datagrid.DataContext = mainWindow.model;
            mainWindow.Log("Load String table: " + fileName + " Successed!");
            mainWindow.Title = "Twine引擎游戏翻译器  " + fileName;
            if (mainWindow.ChangeTranslatedLanguage(newLang))
            {
                mainWindow.Log("新建String表成功,Path: " + fileName);
            }
            mainWindow.Focusable = true;
        }
    }
}
